﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Services;
using Microsoft.Reporting.WinForms;
using MarginTrading.Common;

namespace MarginTrading.Forms
{
    public partial class InterestStatement : Form
    {
        public InterestStatement()
        {
            InitializeComponent();
        }

        private string GetClientCode()
        {
            string clientCode = string.Empty;

            if (!String.IsNullOrEmpty(txtClientName.Text))
            {
                int startIndex = txtClientName.Text.LastIndexOf('(') + 1;
                int length = txtClientName.Text.LastIndexOf(')') - txtClientName.Text.LastIndexOf('(') - 1;
                if (length > 0)
                {
                    clientCode = txtClientName.Text.Substring(startIndex, length);
                }
            }
            else
            {
                string strClientCode = "{0}-{1}-{2}-{3}";
                clientCode = string.Format(strClientCode, txtClientIDPrefix.Text, txtClientID1.Text, txtClientID2.Text,
                    txtClientID3.Text);
            }

            return clientCode;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            if (!String.IsNullOrEmpty(GetClientCode()))
            {
                LoadReport(GetClientCode(), dateTimePicker1.Value.Date, dateTimePicker2.Value.Date);
            }
            button1.Enabled = true;
        }

        private void LoadReport(string clientId,DateTime fromDate, DateTime toDate)
        {
            try
            {
                Client client = ClientService.GetClientByCode(clientId);

                toDate = toDate.AddDays(1);
                toDate = toDate.AddSeconds(-1);
                if (client == null)
                {
                    client = ClientService.GetClientByCode(clientId.ToUpper());
                    if (client == null)
                    {
                        client = new Client();
                    }
                    else
                    {
                        clientId = clientId.ToUpper();
                    }

                }
                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";
                ds.Value = ReportServices.GetInterestByClientAndDateRange(clientId, fromDate, toDate);
                double cumInterest = ReportServices.GetCumInterest(clientId, fromDate);

                this.reportViewer1.LocalReport.ReportPath = "InterestStatement.rdlc";
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.DataSources.Add(ds);

                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }

                //TODO:Remove this 
                AgentCode = Reports.AGENT_CODE;

                this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("CumInterest",cumInterest.ToString()),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("ClientID",client.Code),
                        new ReportParameter("Address",string.Concat(client.Address_1," ",client.Address_2," ",client.Address_3," ",client.Address_4)),
                        new ReportParameter("LoanAccountDate",toDate.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("AdvisorID",client.Advisor),
                        new ReportParameter("Name",string.Concat(client.Initials,
                                                            " ", client.Surname) ),
                        new ReportParameter("ReportTitle",string.Format(Reports.STATEMENT_OF_INTEREST,dateTimePicker1.Value.ToString(DateFormat.DATE_FORMAT),dateTimePicker2.Value.ToString(DateFormat.DATE_FORMAT)))});

                this.reportViewer1.RefreshReport();
                reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = ZoomMode.Percent;
                reportViewer1.ZoomPercent = 100;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToEventLog(e.Message);
            }
        }

        private void InterestStatement_Load(object sender, EventArgs e)
        {
            AutoCompleteStringCollection clientNames = ClientService.GetClientNameCollection();
            if (clientNames != null)
            {
                txtClientName.AutoCompleteCustomSource = clientNames;
            }
        }

        private void txtClientName_TextChanged(object sender, EventArgs e)
        {
            txtClientIDPrefix.Text = string.Empty;
            txtClientID1.Text = string.Empty;
            txtClientID2.Text = string.Empty;
            txtClientID3.Text = string.Empty;
        }

        private void txtClientIDPrefix_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            if (txtClientIDPrefix.Text.Length == 3)
            {
                txtClientID1.Focus();
            }
        }

        private void txtClientID1_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            if (txtClientID1.Text.Length == 9)
            {
                txtClientID2.Focus();
            }
        }

        private void txtClientID2_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            if (txtClientID2.Text.Length == 2)
            {
                txtClientID3.Focus();
            }
        }

        private void txtClientID3_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
        }
    }
}
